package com.sofwin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.sofwin.pojo.Adrs;
import com.sofwin.pojo.Enterprise;
import com.sofwin.pojo.Status;
import com.sofwin.pojo.User;
import com.sofwin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class LoginTestController extends BaseController {
    @Autowired
    private TestService testService;

    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private AdrsService adrsService;
    @Autowired
    private StatusService statusService;
//    @Autowired
//    private SysLoginService sysLoginService;

    @PostMapping("/login")
    public AjaxResult doLogin(@RequestBody User user, HttpServletRequest request){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getuName,user.getuName())
                .eq(User::getPassword,user.getPassword());
        User one = userService.getOne(wrapper);
        if (StringUtils.isNull(one)){
            return AjaxResult.error("登录失败");
        }
        HttpSession session = request.getSession();
        session.setAttribute("user",one.getuName());
        return AjaxResult.success("登录成功",one);
    }

    // 注册
    @PostMapping("/register")
    public AjaxResult register(@RequestBody User user) {
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper/*.eq(User::getUName, user.getUName())*/
//                .eq(User::getPassword, user.getPassword());
//        User one = userService.getOne(wrapper);
//        if (StringUtils.isNotNull(one)) {
//            return AjaxResult.error("用户已存在，请登录");
//        }
        userService.insertUser(user);
        return AjaxResult.success("注册成功");
    }

    //注册的时候返回企业列表
    @GetMapping("/entList")
    public TableDataInfo entList() {
        LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
        //只查询id，name两列数据
        wrapper.select(Enterprise::getEId, Enterprise::getEName);
        //查询条数
        long count = enterpriseService.count();
        Page<Enterprise> page = enterpriseService.page(new Page<Enterprise>(1, count));
        return getDataTable(page.getRecords(), page.getTotal());
    }

    //注册的时候查询地址
    @GetMapping("/adrsList")
    public AjaxResult adrsList(Adrs adrs) {
        //查询条数
        List<Adrs> list = adrsService.getAdrsList(adrs);
        return AjaxResult.success(list);
    }

    //注册的时候显示状态
    @GetMapping("/statusList")
    public AjaxResult statusList(Status status) {
        //查询条数
        List<Status> list = statusService.getStatusList(status);
        return AjaxResult.success(list);
    }




}
